<template>
  <div class="app-container">
    <el-form :model="form">
      <el-form-item label="项目名称" label-width="120px">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="项目描述" label-width="120px">
        <el-input v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="项目地址" label-width="120px">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="GitHub地址" label-width="120px">
        <el-input v-model="form.github"></el-input>
      </el-form-item>
      <el-form-item label="预览图" label-width="120px">
        <Upload v-model="form.thumb" />
      </el-form-item>
      <el-form-item label="排序等级" label-width="120px">
        <el-select
          v-model="form.order"
          label-width="120px"
          placeholder="分类等级"
        >
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="5" value="5"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button
      style="margin-left: 120px"
      type="primary"
      plain
      @click="addProjectHandle"
      >发布项目</el-button
    >
  </div>
</template>

<script>
import Upload from "@/components/Upload.vue";
import { addProject } from "@/api/project.js";

export default {
  data() {
    return {
      listLoading: false,
      dialogVisible: true,
      form: {
        name: "",
        description: "",
        url: "",
        github: "",
        thumb: "",
        order: 1,
      },
      isSend: true,
    };
  },
  components: {
    Upload,
  },
  methods: {
    addProjectHandle() {
      let obj = { ...this.form };
      obj.description = obj.description.split(",");
      obj.order = parseInt(obj.order);
      obj.thumb = obj.thumb.replace(
        /^[a-z]{4,5}\:\/{2}[a-z]{1,}\:[0-9]{1,3}.(.*)/,
        "$1"
      );
      for (let item in obj) {
        this.isSend = true;
        if (!obj[item]) {
          this.$message({
            message: `${item}数据为空,请检查后重试`,
            type: "error",
          });
          this.isSend = false;
          break;
        }
      }
      if (this.isSend) {
        addProject(obj)
          .then(() => {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.$router.push("/projectList");
          })
          .catch((err) => {
            this.$message({
              message: "添加失败",
              type: "error",
            });
          });
      }
    },
  },
};
</script>

<style>
.block {
  margin: 15px 0;
  font-weight: 100;
}
</style>